/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.dao.CodeRuleDao;
import com.minierp.dao.TallySetDao;
import com.minierp.err.PermissionDennyException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.CodeRule;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class CodeRuleMACtr extends MultiActionController {
	private CodeRuleDao codeRuleDao;
	private TallySetDao tallySetDao;
	private String mainView;
	private String listView;

	public ModelAndView handleMain(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.mainView);
		String listType = request.getParameter("list_type");
		if (listType != null && listType.equals("2")) {
			if (!EncCommonHelper.checkPrivilege(request, "BILL_CODE")) {
				throw new PermissionDennyException("Permission Denny");
			}
		} else if (listType != null && listType.equals("1")
				&& !EncCommonHelper.checkPrivilege(request, "DATA_CODE")) {
			throw new PermissionDennyException("Permission Denny");
		}

		mv.addObject("listType", listType);
		return mv;
	}

	public ModelAndView handleList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.listView);
		String listType = request.getParameter("list_type");
		List list = this.codeRuleDao.getList();
		ArrayList arr = new ArrayList();

		for (int i = 0; i < list.size(); ++i) {
			CodeRule c = (CodeRule) list.get(i);
			if (listType != null && listType.equals("1")) {
				if (c.getDataType() == 1 || c.getDataType() == 2) {
					arr.add(c);
				}
			} else if (c.getDataType() == 3) {
				arr.add(c);
			}
		}

		if (listType == null || !listType.equals("1")) {
			mv.addObject("tallySetList", this.tallySetDao.getList(true));
		}

		mv.addObject("codeRuleList", arr);
		mv.addObject("listType", listType);
		return mv;
	}

	public CodeRuleDao getCodeRuleDao() {
		return this.codeRuleDao;
	}

	public void setCodeRuleDao(CodeRuleDao codeRuleDao) {
		this.codeRuleDao = codeRuleDao;
	}

	public String getListView() {
		return this.listView;
	}

	public void setListView(String listView) {
		this.listView = listView;
	}

	public String getMainView() {
		return this.mainView;
	}

	public void setMainView(String mainView) {
		this.mainView = mainView;
	}

	public TallySetDao getTallySetDao() {
		return this.tallySetDao;
	}

	public void setTallySetDao(TallySetDao tallySetDao) {
		this.tallySetDao = tallySetDao;
	}
}